package cloud.ben.smallscalepigfarmmanagementsystem.service.impl.pig;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.PigDeathRecords;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.PigDiseaseRecords;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.PigFatteningHouseDeathRecords;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.pig.PigDiseaseRecordsMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.pig.PigFatteningHouseDeathRecordsMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.service.pig.IPigFatteningHouseDeathRecordsService;
import cloud.ben.smallscalepigfarmmanagementsystem.utils.RespBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Ben
 * @Date: 2022/4/6 11:26
 */
@Service
public class PigFatteningHouseDeathRecordsServiceImpl implements IPigFatteningHouseDeathRecordsService {

    protected static final Logger logger = LoggerFactory.getLogger(PigFatteningHouseDeathRecordsServiceImpl.class);


    @Autowired
    private PigFatteningHouseDeathRecordsMapper pigFatteningHouseDeathRecordsMapper;


    @Override
    public RespBean insertPigFatteningHouseDeathRecords(PigFatteningHouseDeathRecords pigFatteningHouseDeathRecords) {
        if (pigFatteningHouseDeathRecordsMapper.insert(pigFatteningHouseDeathRecords) == 1){
            logger.info("插入成功");
            return RespBean.ok("插入成功");
        }
        logger.error("系统异常，插入失败");
        return  RespBean.error("系统异常，插入失败");
    }

    @Override
    public RespBean deletePigFatteningHouseDeathRecords(Integer id) {
        QueryWrapper<PigFatteningHouseDeathRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", id);
        List<PigFatteningHouseDeathRecords> list = pigFatteningHouseDeathRecordsMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigFatteningHouseDeathRecordsMapper.delete(queryWrapper)!= 0){
                logger.info("删除成功");
                return  RespBean.ok("删除成功");
            }
            logger.error("系统异常，删除失败");
            return RespBean.error("系统异常，删除失败");
        }
        logger.info("您要删除的数据不存在，删除失败");
        return RespBean.error("您要删除的数据不存在，删除失败");
    }

    @Override
    public RespBean updatePigFatteningHouseDeathRecords(PigFatteningHouseDeathRecords pigFatteningHouseDeathRecords) {
        QueryWrapper<PigFatteningHouseDeathRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", pigFatteningHouseDeathRecords.getId());
        List<PigFatteningHouseDeathRecords> list = pigFatteningHouseDeathRecordsMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigFatteningHouseDeathRecordsMapper.update(pigFatteningHouseDeathRecords, queryWrapper)!= 0){
                logger.info("更新成功");
                return RespBean.ok("更新成功");
            }
            logger.error("系统异常，更新失败");
        }
        logger.error("您要更新的数据不存在，更新失败");
        return RespBean.error("您要更新的数据不存在，更新失败");
    }

    @Override
    public RespBean selectPigFatteningHouseDeathRecordsByPage(Integer pageNumber, Integer pageSize) {
        Page<PigFatteningHouseDeathRecords> page = new Page<>(pageNumber, pageSize);
            IPage<PigFatteningHouseDeathRecords> pigFatteningHouseDeathRecordsIPage =pigFatteningHouseDeathRecordsMapper.selectPage(page, null);
        return RespBean.ok("查询成功",pigFatteningHouseDeathRecordsIPage);
    }


    @Override
    public RespBean selectPigFatteningHouseDeathRecordsNum() {
        return RespBean.ok("查询成功",  pigFatteningHouseDeathRecordsMapper.selectCount(null));
    }
}
